import { ElMessage } from "element-plus";
import dayjs from "dayjs";

import {
  cumulate,
  arrange,
  summary,
  policyCompute,
  stepCut,
} from "@/views/FlowStatus/utils";

import fetchSummaryData from "./fetchSummaryData.js";
import fetchHourlyData from "./fetchHourlyData.js";

export default function (
  replyAP,
  keywords = ["ZULZ", "ZUYB"],
  dataInterval = 4,
  dataStep = 4
) {
  const rawAirportPlanData = replyAP["history"];
  const rawAirportData = replyAP["content"];
  const tagsAirport = replyAP["tagsAirport"];
  const begTime = replyAP["begTime"];

  if (!rawAirportData || rawAirportData.length == 0) {
    ElMessage.warning("出错了，未能检索到数据(⊙o⊙)…");
    return {};
  }

  const isAfter = dayjs().isAfter(dayjs(begTime).add(24, "hour"));

  const cumulatedAirportData = cumulate(rawAirportData, dataInterval, dataStep);
  const cumulatedAirportDataPlan = cumulate(rawAirportPlanData, dataInterval, dataStep);
  
  const arrangedAirportData = arrange(cumulatedAirportData, tagsAirport, begTime, dataInterval, [
    "airport",
    "type",
    "point",
  ]);
  const arrangedAirportPlanData = arrange(
    cumulatedAirportDataPlan,
    tagsAirport,
    begTime,
    dataInterval,
    ["airport", "type", "point"]
  );

  keywords.push("DEP", "ARR");
  const summaryAirportData = summary(cumulatedAirportData, tagsAirport, keywords);
  const summaryAirportPlanData = summary(cumulatedAirportDataPlan, tagsAirport, keywords);

  const icaoCodes = Object.keys(summaryAirportData.airport);
  const tablesummaryAirportData = fetchSummaryData({
    summaryAirportData,
    summaryAirportPlanData,
    icaoCodes,
    isAfter,
  });

  const { tableHourlyHeader, tableHourlyData } = fetchHourlyData({
    arrangedAirportData,
    arrangedAirportPlanData,
    icaoCodes,
    isAfter,
  });

  const targetDate = dayjs(begTime).format("YYYY-MM-DD");

  const output = {
    targetDate,
    rawAirportData,
    rawAirportPlanData,
    cumulatedAirportData,
    cumulatedAirportDataPlan,
    arrangedAirportData,
    arrangedAirportPlanData,
    summaryAirportData,
    summaryAirportPlanData,
    tablesummaryAirportData,
    tableHourlyData,
    tableHourlyHeader,
  };
  // flowStore.classicPolicyScalesAP = output;
  console.log("fetchDataAP", output);
  return output;
}
